﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel;
using NPMonitor.Helpers;

namespace NPMonitor.Web.UserControls
{
    public partial class ScheduleDatails : BaseUserControl
    {
        private string Id
        {
            get
            {
                return this.Request.QueryString["Id"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitViewMode();

            BindData();
        }

        //TODO:Bind Data 
        private void BindData()
        {
            if (null != TaskInfo)
            {
                txtType.Text = TaskInfo.TaskType;
                txtName.Text = TaskInfo.Name;
                txtPoolName.Text = TaskInfo.PoolName;
                txtMachineName.Text = TaskInfo.MachineName;

                chkAutoRetry.Checked = TaskInfo.AutoRetry.Value;
                chkRetry.Checked = TaskInfo.Retry.Value;

                txtMaxRetryTimes.Text = TaskInfo.MaxRetry == 0 ? string.Empty : TaskInfo.MaxRetry.ToString();
                txtRetryTimes.Text = TaskInfo.RetryTimes == 0 ? string.Empty : TaskInfo.RetryTimes.ToString();
                txtDesc.Value = TaskInfo.Description;

                if (TaskInfo.TaskType == TaskType.Workflow.ToString())
                {
                    phWorkflowJobList.Visible = true;

                    var jobList = StoreManager.Current.WorkflowStore.GetWorkflowJobDataByWorkflowId(TaskInfo.TaskId.Value);

                    if (jobList != null || jobList.Count > 0)
                    {
                        rptJobList.DataSource = jobList;
                        rptJobList.DataBind();
                    }
                }

                txtTimeOut.Text = TaskInfo.AbsoluteTimeout == TimeSpan.Zero ? string.Empty : TaskInfo.AbsoluteTimeout.ToString();

                txtRunningType.Text = TaskInfo.ScheduleRunningType;

                if (TaskInfo.ScheduleRunningType == CommonString.Schedule && TaskInfo.ScheduleDetails != null)
                {
                    phNextRetry.Visible = true;

                    phSchedule.Visible = true;

                    var daysOfWeekList = TaskInfo.ScheduleDetails.DaysOfWeek.ToList();

                    lblWeeks.Text = daysOfWeekList.Join(", ");
                    

                    txtScheduleStart.Text = TaskInfo.ScheduleDetails.StartDateTime.ToFormatString();
                    txtDailyStartTime.Text = TaskInfo.ScheduleDetails.DailyStartTime.AsTimeSpan.ToString();
                    txtRepeatEvery.Text = TaskInfo.ScheduleDetails.RepeatIntervalString;
                }

                txtStartTime.Text = TaskInfo.LastStartTime.ToFormatString();
                txtEndTime.Text = TaskInfo.LastEndTime.ToFormatString();
                txtNextStart.Text = TaskInfo.NextStartTime.ToFormatString();
                txtStatus.Text = TaskInfo.Status.ToString();
                txtCreated.Text = TaskInfo.CreatedDate.ToFormatString();
                txtCreateBy.Text = TaskInfo.CreatedBy; 
            }
        }

        private void InitViewMode()
        {
            phNextRetry.Visible = false;
            phWorkflowJobList.Visible = false;
            phSchedule.Visible = false;
        }

        protected JobData TaskInfo
        {
            get
            {
                int id;

                JobData jobInfo = new JobData();

                if (int.TryParse(Id, out id))
                {
                    jobInfo = StoreManager.Current.JobStore.GetTaskInfoById(id);
                }

                return jobInfo;
            }
        }
    }
}